helpers.ts ➔ initHelpers   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
export var isShiftPressed: boolean = false;
2
export var isCtrlPressed: boolean = false;
3
4
export function isHtmlElement(object: any) {
5
    return object instanceof HTMLElement;
6
}
7
8
export function initHelpers(): void {
9
    document.addEventListener('keydown', event => handleKeyDown(event));
10
    document.addEventListener('keyup', event => handleKeyUp(event));
11
}
12
13
export function onReady(fn: () => void) {
14
    if (document.readyState != 'loading') {
15
        fn();
16
    } else {
17
        document.addEventListener('DOMContentLoaded', fn);
18
    }
19
}
20
21
export function assigned(obj: any): boolean {
22
    return !(typeof obj === 'undefined' || obj === null);
23
}
24
25
function handleKeyDown(event: KeyboardEvent) {
26
    handleKeyToggle(event, true);
27
}
28
29
function handleKeyUp(event: KeyboardEvent) {
30
    handleKeyToggle(event, false);
31
}
32
33
function handleKeyToggle(event: KeyboardEvent, isPressed: boolean) {
34
    if (event.key === 'Shift') {
35
        isShiftPressed = isPressed;
36
    } else if (event.key === 'Control') {
37
        isCtrlPressed = isPressed;
38
    }
39
}
40
41
export function findTextNodes(baseNode: Node): Array<Node> {
42
    if (!assigned(baseNode)) {
43
        baseNode = document.documentElement;
44
    }
45
46
    let walker = document.createTreeWalker(baseNode, NodeFilter.SHOW_TEXT, null);
47
    let node;
48
    let results = [];
49
    while (node = walker.nextNode()) {
50
        results.push(node);
51
    }
52
53
    return results;
54
}